package com.lc.luzhengtang.activity;

import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.lc.luzhengtang.BaseApplication;
import com.lc.luzhengtang.R;
import com.lc.luzhengtang.adapter.ProfitListAdapter2;
import com.lc.luzhengtang.base.BaseActivity;
import com.lc.luzhengtang.bean.AutherBean;
import com.lc.luzhengtang.bean.CommenBean;
import com.lc.luzhengtang.bean.ShouquanBean;
import com.lc.luzhengtang.conn.ConnAuther;
import com.lc.luzhengtang.conn.ConnSubmitAuther;
import com.lc.luzhengtang.conn.ConnTamList;
import com.lc.luzhengtang.util.LovelyToast;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.scale.ScaleScreenHelperFactory;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/6/20.
 */

public class TeamActivityCommonListActivity extends BaseActivity implements BaseQuickAdapter.RequestLoadMoreListener, SwipeRefreshLayout.OnRefreshListener {

    @BindView(R.id.img_news)
    ImageView imgNews;
    @BindView(R.id.text_normal_person)
    TextView textNormalPerson;
    @BindView(R.id.recyclerView_list)
    RecyclerView recyclerViewList;

    List<ShouquanBean.DataBeanX.DataBean> recycle_list;
    ProfitListAdapter2 profitListAdapter;
    @BindView(R.id.refreshLayout)
    SwipeRefreshLayout refreshLayout;
    public int currentPage = 1;
    private PopupWindow popWindow;
    private TextView etname, tv01, tv02, tv03;

    public  String type1;
    public  String type2;
    public String autherId;

    String item_id;
    List<AutherBean.DataBean> dataBeanList;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_team_normal);
        ButterKnife.bind(this);
        recycle_list = new ArrayList<>();
        dataBeanList = new ArrayList<>();
        recyclerViewList.setLayoutManager(new LinearLayoutManager(this));
        initData();

//        getData();

        profitListAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {

                item_id = recycle_list.get(position).getId() + "";
                showPopupWindow(view);
            }
        });

    }


    private void showPopupWindow(View parent) {
        if (popWindow == null) {

            View view = ScaleScreenHelperFactory.getInstance().loadView((ViewGroup) LayoutInflater.from(this).inflate(R.layout.pop_auther, null));
            //LayoutParams相当于一个Layout的信息包，它封装了Layout的位置、高、宽等信息。
            popWindow = new PopupWindow(view, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, true);
            initPop(view);
        }
        //设置动画效果
        popWindow.setAnimationStyle(android.R.style.Animation_InputMethod);
        //获取popwindow焦点
        popWindow.setFocusable(true);
        //设置popwindow如果点击外面区域，便关闭。
        popWindow.setOutsideTouchable(true);
        popWindow.setBackgroundDrawable(new BitmapDrawable());
        //实现软键盘不自动弹出,ADJUST_RESIZE属性表示Activity的主窗口总是会被调整大小，从而保证软键盘显示空间。
        popWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        //设置popwindow显示位置
        popWindow.showAtLocation(parent, Gravity.CENTER, 0, 0);
    }

    public void initPop(final View view) {
     /*    final String type1 ="";
        String type2;*/

        tv01 = (TextView) view.findViewById(R.id.tv_01);//拍照
        tv02 = (TextView) view.findViewById(R.id.tv_02);//相册
        new ConnAuther(BaseApplication.basePreferences.readUid(), new AsyCallBack<AutherBean>() {
            @Override
            public void onStart(int type) throws Exception {
                super.onStart(type);
            }

            @Override
            public void onSuccess(String toast, int type, Object o, AutherBean commenBean) throws Exception {
                super.onSuccess(toast, type, o, commenBean);
//                type1 =commenBean.getData().get(0).getStsing();
                type1 = commenBean.getData().get(0).getStsing();
                type2 = commenBean.getData().get(1).getStsing();

                tv01.setText(type1);
                tv02.setText(type2);
                dataBeanList = commenBean.getData();

            }

            @Override
            public void onFail(String toast, int type) throws Exception {
                super.onFail(toast, type);
            }

            @Override
            public void onEnd(String toast, int type) throws Exception {
                super.onEnd(toast, type);
            }
        }

        ).execute(TeamActivityCommonListActivity.this, false);


        LinearLayout layout_picselected = (LinearLayout) view.findViewById(R.id.layout_picselected);
        layout_picselected.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popWindow.dismiss();
            }
        });


        tv01.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                popWindow.dismiss();
//                startCamera(mineHeadIv);
                autherId = "1";
                submitShouquan();
            }
        });
        tv02.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                popWindow.dismiss();
//                startAlbum(mineHeadIv);

                autherId = "2";
                submitShouquan();
            }
        });


    }

    public void submitShouquan() {
        new ConnSubmitAuther(autherId, item_id, new AsyCallBack<CommenBean>() {
            @Override
            public void onStart(int type) throws Exception {
                super.onStart(type);
            }

            @Override
            public void onSuccess(String toast, int type, Object o, CommenBean commenBean) throws Exception {
                super.onSuccess(toast, type, o, commenBean);
                LovelyToast.makeText(TeamActivityCommonListActivity.this, commenBean.getMessage(), 0, LovelyToast
                        .SUCCESS, LovelyToast.SCALE);
                onRefresh();

            }

            @Override
            public void onFail(String toast, int type) throws Exception {
                super.onFail(toast, type);
            }

            @Override
            public void onEnd(String toast, int type) throws Exception {
                super.onEnd(toast, type);
            }
        }

        ).execute(TeamActivityCommonListActivity.this, false);
    }


    protected void initData() {
        recyclerViewList.setItemAnimator(new DefaultItemAnimator());
        //recyclerView.setLayoutManager(new LinearLayoutManager(context));
        recyclerViewList.setLayoutManager(new LinearLayoutManager(this));

        profitListAdapter = new ProfitListAdapter2(null);
        profitListAdapter.openLoadAnimation(BaseQuickAdapter.SCALEIN);
        profitListAdapter.isFirstOnly(false);
//        Drawable drawable = ContextCompat.getDrawable(TeamActivityCommonListActivity.this, R.drawable.line_divider);
//        recyclerViewList.addItemDecoration(new SimpleDividerItemDecoration(TeamActivityCommonListActivity.this, drawable, 1));

        recyclerViewList.setAdapter(profitListAdapter);

        refreshLayout.setColorSchemeColors(Color.RED, Color.BLUE, Color.GREEN);
        refreshLayout.setOnRefreshListener(this);
        profitListAdapter.setOnLoadMoreListener(this);

        //开启loading,获取数据
        setRefreshing(true);
        onRefresh();
    }

    public void setRefreshing(final boolean refreshing) {
        refreshLayout.post(new Runnable() {
            @Override
            public void run() {
                refreshLayout.setRefreshing(refreshing);
            }
        });
    }

   public void getData() {
        new ConnTamList(BaseApplication.basePreferences.readUid(), getIntent().getStringExtra("role_id"), "1", new AsyCallBack<ShouquanBean>() {
            @Override
            public void onSuccess(String toast, int type, Object o, ShouquanBean s) throws Exception {
                super.onSuccess(toast, type, o, s);
                recycle_list = s.getData().getData();

                profitListAdapter = new ProfitListAdapter2(recycle_list);
                recyclerViewList.setAdapter(profitListAdapter);

            }

            @Override
            public void onFail(String toast, int type) throws Exception {
                super.onFail(toast, type);
            }

            @Override
            public void onEnd(String toast, int type) throws Exception {
                super.onEnd(toast, type);
            }
        }


        ).execute(TeamActivityCommonListActivity.this, false);
    }


    @OnClick({R.id.img_news, R.id.text_normal_person, R.id.recyclerView_list})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_news:
                finish();
                break;
            case R.id.text_normal_person:
                break;
            case R.id.recyclerView_list:
                break;
        }
    }

    @Override
    public void onRefresh() {
        new ConnTamList(BaseApplication.basePreferences.readUid(), getIntent().getStringExtra("role_id"), "1", new AsyCallBack<ShouquanBean>() {
            @Override
            public void onStart(int type) throws Exception {
                super.onStart(type);
                profitListAdapter.setEnableLoadMore(false);
            }

            @Override
            public void onSuccess(String toast, int type, Object o, ShouquanBean s) throws Exception {
                super.onSuccess(toast, type, o, s);

                recycle_list = s.getData().getData();

                if (recycle_list != null) {
                    currentPage = 1;
                    profitListAdapter.setNewData(recycle_list);
                }
//                textNormalPerson.setText("普通会员" + "(" + recycle_list.size() + "" + ")");
                if (recycle_list.size()==0){
                    textNormalPerson.setText(getIntent().getStringExtra("role_name"));
                }else {
                    textNormalPerson.setText(getIntent().getStringExtra("role_name")+ "（" + recycle_list.size() + "" +"）");
                }

            }

            @Override
            public void onFail(String toast, int type) throws Exception {
                super.onFail(toast, type);
            }

            @Override
            public void onEnd(String toast, int type) throws Exception {
                super.onEnd(toast, type);
                profitListAdapter.removeAllFooterView();
                //最后调用结束刷新的方法
                setRefreshing(false);
            }
        }


        ).execute(TeamActivityCommonListActivity.this, false);
    }

    @Override
    public void onLoadMoreRequested() {
        new ConnTamList(BaseApplication.basePreferences.readUid(), getIntent().getStringExtra("role_id"), "1", new AsyCallBack<ShouquanBean>() {
            @Override
            public void onStart(int type) throws Exception {
                super.onStart(type);
                showLoading();
                profitListAdapter.setEnableLoadMore(true);
            }

            @Override
            public void onSuccess(String toast, int type, Object o, ShouquanBean s) throws Exception {
                super.onSuccess(toast, type, o, s);
                /*recycle_list = s.getData().getData();

                profitListAdapter = new ProfitListAdapter2(recycle_list);
                recyclerViewList.setAdapter(profitListAdapter);
*/

              /*  recycle_list = s.getData().getData();

                if (recycle_list != null) {
                    currentPage = 1;
                    profitListAdapter.setNewData(recycle_list);
                }
*/

                recycle_list = s.getData().getData();

                if (recycle_list != null && recycle_list.size() > 0) {
                    currentPage++;
                    profitListAdapter.setNewData(recycle_list);
                } else {
                    //显示没有更多数据
                    // newsAdapter.loadComplete();
                    profitListAdapter.loadMoreComplete();
                    View noDataView = LayoutInflater.from(TeamActivityCommonListActivity.this).inflate(R.layout.item_no_data, (ViewGroup) recyclerViewList.getParent(), false);
                    profitListAdapter.addFooterView(noDataView);
                }


            }

            @Override
            public void onFail(String toast, int type) throws Exception {
                super.onFail(toast, type);
            }

            @Override
            public void onEnd(String toast, int type) throws Exception {
                super.onEnd(toast, type);
                refreshLayout.setRefreshing(false);
                dismissLoading();
                profitListAdapter.removeAllFooterView();
                //最后调用结束刷新的方法
                profitListAdapter.setEnableLoadMore(false);
                setRefreshing(false);
            }
        }


        ).execute(TeamActivityCommonListActivity.this, false);
    }
}
